package com.example.usharingtest.fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.usharingtest.R;

import com.example.usharingtest.model.User;
import com.example.usharingtest.service.ServiceUrl;
import com.example.usharingtest.util.ACache;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.squareup.picasso.Picasso;

import butterknife.OnClick;

/**
 * Created by X on 2017/9/3.
 */
public class MyInfoFragment extends Fragment {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        SharedPreferences preference = getActivity().getSharedPreferences("userdata", getActivity().MODE_PRIVATE);
        int u_id = preference.getInt("userid",-1);

        /*
        * 登录界面未实现 实现后进行异步请求验证用户
        * 1.登录采取邮箱登录
        * 2.注册采取邮箱或手机注册
        * 3.登录提交密码时必须进行加密处理*/

        //int id =preference.getInt("userid",-1);
        Log.d("sharedreference userid",String.valueOf(1));

        /*得到View实例*/
        View view = inflater.inflate(R.layout.mine, container, false);
        if(u_id == -1){
            TextView userid = (TextView)view.findViewById(R.id.user_ID);
            ImageView userphoto = (ImageView)view.findViewById(R.id.user_headportrait) ;

            userid.setText("用户未登录");
            Picasso.with(getContext()).load(R.drawable.clear).placeholder(R.drawable.loading).error(R.drawable.see).into(userphoto);
        }else {
            initUserinfo(u_id);
        }
        //TODO 设置部分 fagment尚未完成
        /*ImageView setting = (ImageView)view.findViewById(R.id.user_setting);

        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

                SettingFragment settingFragment = new SettingFragment();
                fragmentTransaction.replace(R.id.study_fragment,settingFragment);
                fragmentTransaction.commit();
            }
        });*/

        return view;
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    public void initUserinfo(int userid){
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        String url = ServiceUrl.getUrl(getActivity(), R.string.URL_USER_INFO);

        RequestParams parameters = new RequestParams();
        parameters.put("userid",String.valueOf(userid));
        System.out.println("URL="+url);

        asyncHttpClient.get(url,parameters,new AsyncHttpResponseHandler(){
            @Override
            public void onSuccess(String content) {
                //请求相应成功回调事件
                Log.d("内容", content);
                Toast.makeText(getActivity(), "服务器连接成功", Toast.LENGTH_SHORT).show();
                JSONObject jsonObject = JSONObject.parseObject(content);
                boolean success = jsonObject.getBoolean("success");
                if(success){
                    String result = jsonObject.getString("data");
                    User user = JSON.parseObject(result,User.class);
                    //填充用户信息
                    TextView userid = (TextView)getView().findViewById(R.id.user_ID);
                    TextView username = (TextView)getView().findViewById(R.id.user_name);
                    TextView usersign = (TextView)getView().findViewById(R.id.user_sign);
                    ImageView userphoto = (ImageView)getView().findViewById(R.id.user_headportrait) ;
                    Log.d("userinfo",user.getUsername()+user.getUserphotopath());
                    userid.setText("用户邮箱:"+user.getUseremail());
                    username.setText("用户昵称:"+user.getUsername());
                    usersign.setText("个性签名:"+user.getUserphone());
                    Picasso.with(getContext()).load(getContext().getString(R.string.URL_SERVER)+user.getUserphotopath()).placeholder(R.drawable.loading).error(R.drawable.see).into(userphoto);

                }

            }

            @Override
            public void onFailure(Throwable error, String content) {
                super.onFailure(error, content);
                Toast.makeText(getActivity(), "服务器连接失败", Toast.LENGTH_SHORT).show();
                //请求响应失败回调事件

            }
        });
    }

}
